package nige.capture;

import java.awt.Color;
import java.awt.Image;
import java.awt.image.ImageObserver;
import java.awt.image.PixelGrabber;
import java.io.File;

import javax.imageio.ImageIO;

/**
 * 
 * @author Nicolas
 *
 */
public class Scan extends Thread {
	
	public static int delta = 700;
	public static int pas = 10;
	
	public Scan() {
		start();
	}
	
	@Override
	public void run() {
		copieImage();
		parcoursGD();
		parcoursDG();
		Maillage.createMaillage();
		Fenetre.imageTransformee.repaint();
	}
	
	public static void parcoursGD() {
		try {
			Image img = ImageIO.read(new File(JPaneImage.imageScan));
			Color[][] colTab = JPaneTransformation.colorImage;
			Color lastCol = Color.white;
			Color currentCol = Color.white;
			for(int i=0;i<img.getWidth(null);i+=pas) {
				for(int j=0;j<img.getHeight(null);j++) {
					currentCol = colTab[i][j];
					if(Math.abs(getValue(currentCol)-getValue(lastCol)) > delta) {
						if(j-1 >= 0) {
							colTab[i][j-1] = lastCol;
						}
						else if(i-1 >= 0) {
							colTab[i-1][0] = lastCol;
						}
						lastCol = currentCol;
						colTab[i][j] = lastCol;
					}
					else {
						colTab[i][j] = Color.white;
					}
				}
			}
			for(int j=0;j<img.getHeight(null);j+=pas) {
				for(int i=0;i<img.getWidth(null);i++) {
					currentCol = colTab[i][j];
					if(Math.abs(getValue(currentCol)-getValue(lastCol)) > delta) {
						Maillage.points.add(
								new Points(i-1,j,0,lastCol)
								);
						lastCol = currentCol;
						Maillage.points.add(
								new Points(i,j,0,lastCol)
								);
					}
				}
			}
		}
		catch(Exception e) {
			System.out.println(e);
		}
	}

	public static void parcoursDG() {
		try {
			Image img = ImageIO.read(new File(JPaneImage.imageScan));
			Color[][] colTab = JPaneTransformation.colorImage;
			Color lastCol = Color.white;
			Color currentCol = Color.white;
			for(int j=img.getHeight(null)-1;j>-1;j-=pas) {
				for(int i=img.getWidth(null)-1;i>-1;i--) {
					currentCol = colTab[i][j];
					if(Math.abs(getValue(currentCol)-getValue(lastCol)) > delta) {
						colTab[i+1][j] = lastCol;
						lastCol = currentCol;
						colTab[i][j] = lastCol;
					}
					else {
						colTab[i][j] = Color.white;
					}
				}
			}
			for(int i=img.getWidth(null)-1; i>-1; i-=pas) {
				for(int j=img.getHeight(null)-1; j>-1; j--) {
					currentCol = colTab[i][j];
					if(Math.abs(getValue(currentCol)-getValue(lastCol)) > delta) {
						Maillage.points.add(
								new Points(i,j+1,0,lastCol)
								);
						lastCol = currentCol;
						Maillage.points.add(
								new Points(i,j,0,lastCol)
								);
					}
				}
			}
		}
		catch(Exception e) {
			System.out.println(e);
			e.printStackTrace();
		}
	}
	
	public static int getValue(Color col) {
		return col.getBlue()+col.getGreen()+col.getRed()+col.getAlpha();
	}
	
	public static void copieImage() {
		try {
			Image img = ImageIO.read(new File(JPaneImage.imageScan));
			JPaneTransformation.colorImage = 
				new Color[img.getWidth(null)][img.getHeight(null)];
			handlepixels(img,0,0,img.getWidth(null), img.getHeight(null));
		}
		catch(Exception e) {
			System.out.println(e);
			e.printStackTrace();
		}
	}
	
	/**
	 * FROM : http://java.sun.com/j2se/1.4.2/docs/api/java/awt/image/PixelGrabber.html
	 * HELP : http://www.neuralsemantics.com/applets/src/cbrowser_source.html
	 */
	public static Color handlesinglepixel(int x, int y, int pixel) {
		int alpha = (pixel >> 24) & 0xff;
		int red   = (pixel >> 16) & 0xff;
		int green = (pixel >>  8) & 0xff;
		int blue  = (pixel      ) & 0xff;
		return new Color(red,green,blue,alpha);
	 }

	/**
	 * FROM : http://java.sun.com/j2se/1.4.2/docs/api/java/awt/image/PixelGrabber.html
	 * HELP : http://www.neuralsemantics.com/applets/src/cbrowser_source.html
	 */
	public static void handlepixels(Image img, int x, int y, int w, int h) {
		int[] pixels = new int[w * h];
		PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w);
		try {
			pg.grabPixels();
		}
		catch (InterruptedException e) {
			System.err.println("interrupted waiting for pixels!");
			return;
		}
		if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
			System.err.println("image fetch aborted or errored");
			return;
		}
		for (int j = 0; j < h; j++) {
			for (int i = 0; i < w; i++) {
				JPaneTransformation.colorImage[i][j] = 
					handlesinglepixel(x+i, y+j, pixels[j * w + i]);
			}
		}
	}
}
